bmgadget =
{
	config:
	{
		read : function()
		{
		},
		save : function()
		{
		}
	},
	settings:
	{
		display : function()
		{
		},
		update : function()
		{
		}
	},
	flyout:
	{
		display : function()
		{
		},
		keypress : function()
		{
		}
	},
	dockpanel:
	{
		keypress: function()
		{
		    if (System.Gadget.docked) return;
		    
		    switch(event.keyCode)
		    {
		        case 37:
		        case 100:
		            bmgadget.onAction("prev");
		            break;
		        case 39:
		        case 102:
		            bmgadget.onAction("next");
		            break;		        
		    }		   
		}
	},
	init : function()
	{
		feedmgr = new ActiveXObject( "Microsoft.FeedsManager" );
		
		for (var feedIndex = 0; feedIndex < feedmgr.RootFolder.Feeds.Count; feedIndex++)
            	{
            		if ( feedmgr.RootFolder.Feeds.Item(feedIndex).Name == "Background Motion" )
            		{            			
            			bmgadget.bmfeed = feedmgr.RootFolder.Feeds.Item(feedIndex);
            			break;
            		}			
            	}    
            	
            	if ( bmgadget.bmfeed == null )
            	{
            		docked.innerHTML = "No feed..";
            	}        		
	},	
	load : function()
	{	
		if ( bmgadget.bmfeed != null )
		{
			this.timer = setInterval("bmgadget.timerEvent()", 10000);	
		}		
	},
	timerEvent: function()
	{
		if ( !bmgadget.timerIsActive ) return;
		bmgadget.getNextItem();
		bmgadget.display();
	},
	display : function()
	{
		if ( bmgadget.bmfeed == null ) return;
		
		if ( bmgadget.bmfeed.Items.item(this.currentIndex).Enclosure == null )
		{
			return;
		}
		
		var imagePathAndName = bmgadget.bmfeed.Items.item(bmgadget.currentIndex).Enclosure.LocalPath;
										
		if ( System.Gadget.docked )
		{			
			dockpicture.src = "gimage:///" + encodeURIComponent(imagePathAndName) + "?width=130&height=100";
		}
		else
		{
			undockpicture.src = "gimage:///" + encodeURIComponent(imagePathAndName) + "?width=360&height=280";
			undocktitle.innerHTML = bmgadget.bmfeed.Items.item(bmgadget.currentIndex).Title;
			
			var id = bmgadget.bmfeed.Items.item(bmgadget.currentIndex).Guid;
			id = id.substring( id.lastIndexOf( "/" ) + 1 );
			undocklink.href = "http://backgroundmotion/View.aspx?id=" + id;
		}					
	},
	getNextItem : function()
	{
		if ( bmgadget.bmfeed == null ) return;
			
		bmgadget.currentIndex++;		
		if ( bmgadget.currentIndex >= bmgadget.bmfeed.Items.Count ) bmgadget.currentIndex = 0;													
	},
	getPrevItem : function()
	{
		if ( bmgadget.bmfeed == null ) return;
			
		bmgadget.currentIndex--;		
		if ( bmgadget.currentIndex < 0 ) bmgadget.currentIndex = bmgadget.bmfeed.Items.Count - 1;
	},	
	docked : function()
	{	
		with(dockpictureFrame.style)
			top=-45,left=-2,width=120, height=90;
		
		this.timerIsActive = true;
		this.getNextItem();
		this.display();
	},
	undocked : function()
	{
		this.timerIsActive = false;
		this.display();
	},
	onAction: function(action)
	{
            switch(action)
            {
        	case "prev":
                    this.getPrevItem();	                    
                    break;
                case "next":	                    
                    this.getNextItem();
                    break;	               
	    }  		
	    
	    this.display();    		    
	},
	timerIsActive : true,
	feedmgr : null,
	bmfeed : null,
	currentIndex : 0,
	timer : null
}